<?php
namespace Modules\Wx\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Wx\Entities\WeConfig;
use Modules\Wx\Http\Requests\WeConfigRequest;
class WeConfigController extends Controller
{
    //显示列表
    public function index()
    {
        $data = WeConfig::paginate(10);
        return view('wx::we_config.index', compact('data'));
    }

    //创建视图
    public function create(WeConfig $we_config)
    {
        //获取wxconfig表中的微信配置信息的数据
        $data = $we_config->pluck('value','name');
        return view('wx::we_config.create',compact('data'));
    }

    //保存数据
    public function store(WeConfigRequest $request,WeConfig $we_config)
    {
        //$we_config->fill($request->all());
        //$we_config->save();
        //在进行数据添加之前,将wxconfig表中的数据截断,然后再添加
        $we_config->truncate();
        foreach ($request->all() as $name => $value)
            if ($name != '_token') {
                $we_config->create(
                    [
                        'name' => $name,
                        'value' => $value,
                    ]
                );
            }

        return redirect('/wx/we_config')->with('success', '保存成功');
    }

    //显示记录
    public function show(WeConfig $field)
    {
        return view('wx::we_config.show', compact('field'));
    }

    //编辑视图
    public function edit(WeConfig $we_config)
    {
        return view('wx::we_config.edit', compact('we_config'));
    }

    //更新数据
    public function update(WeConfigRequest $request, WeConfig $we_config)
    {
        $we_config->update($request->all());
        return redirect('/wx/we_config')->with('success','更新成功');
    }

    //删除模型
    public function destroy(WeConfig $we_config)
    {
        $we_config->delete();
        return redirect('wx/we_config')->with('success','删除成功');
    }
}
